package com.zbkj.crmeb.finance.controller;

import java.math.BigDecimal;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.common.CommonPage;
import com.common.PageParamRequest;
import com.crmeb.core.common.CommonResult;
import com.utils.CrmebUtil;
import com.zbkj.crmeb.chant.service.IChannelMerchantAccountService;
import com.zbkj.crmeb.chant.service.IChannelMerchantService;
import com.zbkj.crmeb.finance.model.UserRecharge;
import com.zbkj.crmeb.finance.request.GetListRequest;
import com.zbkj.crmeb.finance.request.UserRechargeSearchRequest;
import com.zbkj.crmeb.finance.response.UserRechargeResponse;
import com.zbkj.crmeb.finance.service.UserRechargeService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;


/**
 * 用户充值表 前端控制器
 */
@Slf4j
@RestController
@RequestMapping("api/admin/user/topUpLog")
@Api(tags = "财务 -- 充值")

public class UserRechargeController {

    @Autowired
    private UserRechargeService userRechargeService;
    @Autowired
    private IChannelMerchantAccountService channelMerchantAccountService;
    @Autowired
    private IChannelMerchantService channelMerchantService;

    @ApiOperation(value = "分页列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<UserRechargeResponse>>  getList(@Validated UserRechargeSearchRequest request, @Validated PageParamRequest pageParamRequest){
        CommonPage<UserRechargeResponse> userRechargeCommonPage = CommonPage.restPage(userRechargeService.getList(request, pageParamRequest));
        return CommonResult.success(userRechargeCommonPage);
    }

    @ApiOperation(value = "根据交易类型获得分页列表")
    @RequestMapping(value = "/getListByTradeType", method = RequestMethod.GET)
    public CommonResult<CommonPage<UserRechargeResponse>>  getListByTradeType(GetListRequest request){
        CommonPage<UserRechargeResponse> userRechargeCommonPage = CommonPage.restPage(userRechargeService.getListByTradeType(request));
        return CommonResult.success(userRechargeCommonPage);

    }
    
    @ApiOperation(value = "充值提现详情")
    @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
    public CommonResult<UserRechargeResponse>  getList(@PathVariable Integer id){
        return CommonResult.success(userRechargeService.getRechargeInfo(id));
    }

    @ApiOperation(value = "提现审核")
    @PutMapping("/withdrawal/audit")
    public CommonResult<Object> withdrawalAudit(@RequestBody @Validated WithdrawalAuditRequest req, HttpServletRequest request){
        UserRecharge userRecharge = userRechargeService.getById(req.getId());
        if(null == userRecharge) {
            return CommonResult.failed("该记录不存在!");
        }
        if(0 != userRecharge.getStatus()) {
            return CommonResult.failed("该记录已处理!");
        }
        if(-1 != req.getStatus() && 1 != req.getStatus()) {
            return CommonResult.failed("审核状态错误!");
        }
        if(2 != userRecharge.getTradeType()) {
            return CommonResult.failed("该记录非提现申请!");
        }
        boolean b = channelMerchantAccountService.offlineWithdrawAudit(userRecharge, req, CrmebUtil.getClientIp(request));
        return b ? CommonResult.success() : CommonResult.failed();
    }

    /**
     * 删除用户充值表
     * @param id Integer
     * @author Mr.Zhang
     * @since 2020-05-11
     */
    @ApiOperation(value = "删除")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public CommonResult<String> delete(@RequestParam(value = "id") Integer id){
        UserRecharge userRecharge = userRechargeService.getById(id);
        if(userRecharge == null){
            return CommonResult.failed("数据异常");
        }

        if(userRecharge.getPaid()){
            return CommonResult.failed("已支付的订单记录无法删除");
        }

        if(userRechargeService.removeById(id)){
            return CommonResult.success();
        }else{
            return CommonResult.failed();
        }
    }

    /**
     * 充值总金额
     * @author Mr.Zhang
     * @since 2020-05-11
     */
    @ApiOperation(value = "提现总金额")
    @RequestMapping(value = "/balance", method = RequestMethod.POST)
    public CommonResult<HashMap<String, BigDecimal>> balance(){
        return CommonResult.success(userRechargeService.getBalanceList());
    }

}



